﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Media;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 音乐播放器
{
    public partial class Form1 : Form
    {

        String[] musicFile;

        public string[] MusicFile
        {
            get
            {
                return musicFile;
            }

            set
            {
                musicFile = value;
            }
        }

        

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            CheckForIllegalCrossThreadCalls = false;
        }
        /// <summary>
        /// 找到本地音乐文件夹并获取音乐
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void 打开音乐文件夹ToolStripMenuItem_Click(object sender, EventArgs e)
        {

            string folderPath = "";
            FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog();
            folderBrowserDialog.Description = "选择音乐文件夹";
            if (folderBrowserDialog.ShowDialog() == DialogResult.OK)
            {
                folderPath = folderBrowserDialog.SelectedPath;
                musicFile=Directory.GetFiles(folderPath,"*.wav");
                String[] musicName = new String[musicFile.Length];
                for (int i=0;i<musicFile.Length;i++)
                {
                    musicName[i] = Path.GetFileName(musicFile[i]);
                }
                
                foreach (string s in musicName) {
                    listBox1.Items.Add(s);
                }
                
            }
            else
            {
                return;
            }
        }
        /// <summary>
        /// 上一曲
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void UpButton_Click(object sender, EventArgs e)
        {
            Thread th = new Thread(OpenMusic);
            th.IsBackground = true;
            th.Start(UpButton.Text);
        }

        private void OpenMusic(object buttonName) {
            int listIndex = listBox1.SelectedIndex;
            //设置中转值判断是否超过
            int temp = listIndex;
            if (buttonName as String == "上一曲")
            {
                if ((temp -= 1) < 0) return;
                else {
                    listIndex--;
                    listBox1.SelectedIndex--;
                }
            }
            else if (buttonName as String == "下一曲")
            {
                if ((temp += 1) > musicFile.Length-1) return;
                else {
                    listIndex++;
                    listBox1.SelectedIndex++;
                }            
            }
            String mf = musicFile[listIndex];
            SoundPlayer player = new SoundPlayer();
            player.SoundLocation = mf;
            player.Play();
        }
        /// <summary>
        /// 下一曲
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void nextButton_Click(object sender, EventArgs e)
        {
            Thread th = new Thread(OpenMusic);
            th.IsBackground = true;
            th.Start(nextButton.Text);
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            Thread th = new Thread(() => {
                int musicIndex = listBox1.SelectedIndex;
                String mf = musicFile[musicIndex];
                label1.Text = musicIndex +":" + Path.GetFileName(mf);
                SoundPlayer player = new SoundPlayer();
                player.SoundLocation = mf;
                player.Play();
                
            }
            );
            th.IsBackground = true;
            th.Start();
        }
    }
}
